<template>
  <div>
    <headline :headline="'基本信息'" />
    <el-form ref="ruleForm" class="form" :model="goodsInfo" label-width="150px" size="mini" label-position="left">
      <el-row :gutter="gutter">
        <el-col v-for="(item, index) in itemList" :key="index" :span="span" :xl="xl">
          <el-form-item :label="item.label" :prop="item.value" :rules="{ required: item.required, message: `请输入${item.label}`, trigger: 'blur' }">
            <el-input
              v-model="goodsInfo[item.value]"
              :maxlength="item.maxlength"
              :show-word-limit="Boolean(item.maxlength)"
              :disabled="item.disabled || isDisabled"
              @input="inputChange(item.event, $event, false, item.value)"
              @change="inputChange(item.event, $event, true, item.value)"
            >
              <span slot="suffix" class="suffix">{{ item.suffix }}</span>
            </el-input>
            <el-popover :style="item.icon ? '' : 'margin-left: 25px'" placement="bottom" trigger="click">
              <component :is="item.iconTemplate" />
              <i v-if="item.icon" slot="reference" class="el-icon-question" />
            </el-popover>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import headline from '@/views/coi/components/headline.vue'
import eventBus from '@/utils/eventBus'
import tradeName from '../schedule/components/fieldPrompt/tradeName'
import specifications from '../schedule/components/fieldPrompt/specifications.vue'
import skuWeight from '../schedule/components/fieldPrompt/skuWeight.vue'
import { baseInfo } from '@/views/coi/commonJs/goodsInfo.js'

export default {
  components: {
    headline,
    tradeName,
    specifications,
    skuWeight
  },
  props: {
    goodsInfo: {
      type: Object,
      default: () => {
        return {}
      }
    },
    isDisabled: {
      type: Boolean,
      default: true
    },
    fromPage: {
      type: String,
      default: 'making'
    },
    gutter: {
      type: Number,
      default: 80
    },
    span: {
      type: Number,
      default: 12
    },
    xl: {
      type: Number,
      default: 8
    },
    itemList: {
      type: Array,
      default: () => {
        return baseInfo
      }
    }
  },
  data() {
    return {
      count: 0
    }
  },
  watch: {
    goodsInfo: {
      handler(newValue) {
        if (this.count > 0 && this.fromPage === 'commodityLibrary') {
          this.$parent.operationCode[0] = 100
        }
        this.count++
      },
      deep: true
    }
  },
  mounted() {
    if (this.fromPage === 'CBEC') {
      return
    } else {
      this.itemList.forEach((item, i) => {
        if (this.fromPage !== 'making') {
          if (i === 0) {
            item.disabled = true
          }
          if (this.fromPage === 'commodityLibrary') {
            if (i === 1) item.disabled = true
            if (i === 2) {
              item.label = '建品供应商号'
              item.value = 'buildVendorNbr'
              item.required = true
              item.disabled = this.$route.params.type !== 'edit'
            }
          }
        }
      })
    }
  },
  methods: {
    submitForm() {
      let flag = null
      this.$refs.ruleForm.validate((valid) => {
        flag = valid
      })
      return flag
    },
    inputChange(event, data, flag, key) {
      if (event) this[event](data, flag, key)
    },
    skuWeightChange(e, flag) {
      this.goodsInfo.skuWeight = e.match(/^\d*(\.?\d{0,3})/g)[0] || null
      if (e > 5 && flag) {
        this.$alert('商品毛重超过5KG，请核对是否正确！', {
          confirmButtonText: '确定',
          showClose: false,
          callback: () => {}
        })
      }
    },
    skuNameChange(value, flag, key) {
      const obj = {
        key,
        value
      }
      eventBus.$emit('dataChange', obj)
    },
    deptNbrChange(e, flag, key) {
      this.goodsInfo[key] = e.match(/^\d*/g)[0] || null
    },
    specifications(e, flag, key) {
      this.goodsInfo[key] = e.match(/^\d*/g)[0] || null
    }
  }
}
</script>

<style scoped lang="scss">
.form {
  margin: 0 10px;
  margin-top: 20px;
}

/deep/ .el-select {
  width: 100%;
}
.suffix {
  margin-right: 10px;
}
/deep/ .el-form-item__content {
  display: flex;
  align-items: center;
}
.el-icon-question {
  margin-left: 10px;
  cursor: pointer;
  font-size: 16px;
}
</style>
